package com.liuyandog.DBserver;

import com.liuyandog.pojo.Administrator;
import com.liuyandog.pojo.Person;
import com.liuyandog.pojo.User;


import javax.lang.model.type.ArrayType;
import java.sql.SQLException;
import java.util.ArrayList;

public class LoginServer {
    private DBserver dbserver;
    private String userid;
    private String password;

    public LoginServer(String userid, String password) {
        this.userid = userid;
        this.password = password;
    }

    public LoginServer() {
    }

    public Person Check()  {
        try {
        dbserver=new DBserver("SELECT *\n" +
                "FROM library.administrators\n" +
                "WHERE Aid='"+userid+"'");
        ArrayList<String> arrayList= null;
            arrayList = dbserver.select();

        if(arrayList.size()==0){//管理员表查询为空，转入用户查询
            dbserver.setSqlString("SELECT *\n" +
                    "FROM library.users\n" +
                    "WHERE Usid='"+userid+"'");
            arrayList=dbserver.select();
            if(arrayList.size()==0){//用户表查询也空
                return null;
            }
            else {
                dbserver.setSqlString("SELECT UCnumber\n" +
                        "FROM library.ucuserclassify\n" +
                        "WHERE UCtypenumber="+Integer.parseInt(arrayList.get(10)));
                int i=Integer.parseInt(dbserver.select().get(0));
                if(arrayList.get(1).equals(password)) {
                    return new User(Integer.parseInt(arrayList.get(0)),
                            arrayList.get(1), arrayList.get(2),arrayList.get(3),
                           arrayList.get(4),arrayList.get(5),arrayList.get(6),
                            Integer.parseInt(arrayList.get(7))
                            ,arrayList.get(8),0,i);//生成用户对象
                }
            }

        }
        else {//管理员表查询非空，生成管理员
            if(arrayList.get(1).equals(password)) {
                int lever=1;
                if(arrayList.get(7).equals("系统"))lever=2;
                return new Administrator(Integer.parseInt(arrayList.get(0))
                        , arrayList.get(1), arrayList.get(2), arrayList.get(3)
                        , Integer.parseInt(arrayList.get(4)),arrayList.get(5)
                        , arrayList.get(6),lever);//生成管理员对象
            }
        }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
